using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

namespace BToolkit.ExeUtils
{
    public class CopyResToAndroidCache : MonoBehaviour
    {
        IEnumerator Start()
        {
            if (Application.isEditor || Application.platform == RuntimePlatform.Android)
            {
                if (!File.Exists(MainController.resPath + "/config.txt"))
                {
                    if (!Directory.Exists(MainController.resPath + "/01")) Directory.CreateDirectory(MainController.resPath + "/01");
                    if (!Directory.Exists(MainController.resPath + "/02")) Directory.CreateDirectory(MainController.resPath + "/02");

                    yield return CopyFile("01.png");
                    yield return CopyFile("02.png");
                    yield return CopyFile("bg.jpg");
                    yield return CopyFile("btn_clsoe.png");
                    yield return CopyFile("btn_left.png");
                    yield return CopyFile("btn_up.png");
                    yield return CopyFile("config.txt");
                    yield return CopyFile("DataStorage");
                    yield return CopyFile("PingBao.jpg");
                }
            }
            SceneManager.LoadScene("Main");
            yield return null;
        }

        IEnumerator CopyFile(string filePath)
        {
            string sourcePath = Application.streamingAssetsPath + "/Res";
            UnityWebRequest request = UnityWebRequest.Get(sourcePath + "/" + filePath);
            yield return request.SendWebRequest();
            if (request.error != null)
            {
                Debug.LogError(request.error);
            }
            File.WriteAllBytes(MainController.resPath + "/" + filePath, request.downloadHandler.data);
        }
    }
}